package com.zeildx.glife;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.zeildx.glife.entity.Word;

import java.util.ArrayList;
import java.util.List;

public class GetAdapter extends ListAdapter<Word,GetAdapter.MyViewHolder> {
    //List<Word> allGetWords = new ArrayList<>();
    boolean useCardView;
    private getViewModel getViewModel;

    public GetAdapter(boolean useCardView,getViewModel getViewModel) {
        super(new DiffUtil.ItemCallback<Word>() {
            @Override
            public boolean areItemsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
                return oldItem.getId() == newItem.getId();
            }

            @Override
            public boolean areContentsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
                return (oldItem.getWorddate().equals(newItem.getWorddate())
                        && oldItem.getWordschedule().equals(newItem.getWordschedule()));
            }
        });
        this.useCardView = useCardView;
        this.getViewModel = getViewModel;
    }


//    public void setAllGetWords(List<Word> allGetWords) {
//        this.allGetWords = allGetWords;
//    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView;
        if(useCardView){
            itemView = layoutInflater.inflate(R.layout.cellget_card,parent,false);
        }else {
            itemView = layoutInflater.inflate(R.layout.cellget_card,parent,false);
        }
        final MyViewHolder holder = new MyViewHolder(itemView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("https://m.youdao.com/dict?le=eng&q=" + holder.textViewDate.getText());
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(uri);
                holder.itemView.getContext().startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        Word word = getItem(position);
        holder.textViewNumber.setText(String.valueOf(position + 1));
        holder.textViewDate.setText(word.getWorddate());
        holder.textViewSchedule.setText(word.getWordschedule());
    }

    @Override
    public void onViewAttachedToWindow(@NonNull MyViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        holder.textViewNumber.setText(String.valueOf(holder.getAdapterPosition() + 1));
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textViewNumber,textViewDate,textViewSchedule;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewNumber = itemView.findViewById(R.id.textViewNumber);
            textViewDate = itemView.findViewById(R.id.textViewDate);
            textViewSchedule = itemView.findViewById(R.id.textViewSchedule);
        }
    }
}
